package com.erp.order.supply.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.order.purchasing.model.PurchaseOrder;
import com.erp.order.supply.dao.SupplyMaterialDao;
import com.erp.order.supply.model.SupplyMaterial;
import com.erp.order.supply.model.SupplyMaterialVO;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SupplyMaterialDaoImpl extends BaseDaoImpl<SupplyMaterial> implements SupplyMaterialDao {
    /**
     * dao支持
     */
    @Autowired
    private DaoSupport daoSupport;

    @Override
    public SupplyMaterial addCompleteData(SupplyMaterial supplyMaterial) {
        this.daoSupport.insertDataTransaction(supplyMaterial);
        return supplyMaterial;
    }

    @Override
    public SupplyMaterial getDataObject(String s) {
        List<SupplyMaterial> result = daoSupport.selectDataSql(
                "SELECT " +
                        "il.* " +
                        "FROM " +
                        "supply_material il " +
                        "WHERE " +
                        "il.purchase_order_id = :id",
                new HashMap<String, Class<?>>(){{
                    put("il", SupplyMaterial.class);
                }},
                new HashMap<String, Object>(){{
                    put("id", s);
                }});
        return result == null || result.isEmpty() ? null : result.get(0);
    }

    @Override
    public List<SupplyMaterial> getDataObjects(SupplyMaterial supplyMaterial) {
        return null;
    }

    @Override
    public List<SupplyMaterial> getDataObjectsForDataAuth(String s, Pages pages, SupplyMaterial supplyMaterial) {
        return null;
    }

    @Override
    public List<PurchaseOrder> getSupplyLists(Pages pages, PurchaseOrder purchaseOrder, String username) {
        String sql="SELECT * FROM purchase_order p " +
                    "WHERE p.supplier_id=(SELECT m.vendor_id FROM md_vendor m WHERE m.credit_code=:username) " +
                    "AND p.status=1 ";
        sql = sql + " order by p.create_time desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("username", username);
        List<PurchaseOrder> result = daoSupport.getDataSqlByPage(sql, PurchaseOrder.class,args,pages);
        return result;
    }


    @Override
    public List<SupplyMaterialVO> getSupplyAndSupplyRList(Pages pages, SupplyMaterialVO supplyMaterialVO){
       String sql = "select a.id,a.vendor_id,a.purchase_order_id,a.supply_type,a.create_time," +
               "a.create_user,a.update_time,a.update_user,a.is_del,c.id as cId,c.supply_material_id," +
               "c.vendor_id as cvendor_id,c.supply_material_rid,c.material_code,c.num," +
               "c.create_time as ccreate_time,c.create_user as ccreate_user,c.standard,c.brand," +
               "c.standard_unit,c.order,c.update_time as cupdate_time,c.update_user as cupdate_user," +
               "c.is_del as cis_del,c.price,b.id as rid,b.code,b.supply_material_id as rsupply_material_id,"+
               "b.distribute_way,b.send_user,b.send_phone,b.send_time,b.address,b.receiver," +
               "b.receiver_phone,b.express_company,b.express_code,b.is_invoice,b.invoice_type," +
               "b.taxrate,b.total,b.create_time as rcreate_time,b.create_user as rcreate_user," +
               "b.update_time as rupdate_time,b.update_user as rupdate_user,b.is_del as ris_del,b.status,d.vendor_name,d.contact_name "+
               "from supply_material a, supply_material_r b,supply_material_detail c,md_vendor d " +
               "where a.id=b.supply_material_id and b.supply_material_id=c.supply_material_rid and a.vendor_id=d.vendor_id " +
               "and b.status = '0'";
       sql = sql + " order by a.create_time desc";

       List<SupplyMaterialVO> result = daoSupport.getDataSqlByPage(sql,SupplyMaterialVO.class,pages);

        return result;
    }
}
